﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using BezPanike.Domain;
using BezPanike.Domain.Services;

public partial class Admin_Default : System.Web.UI.Page
{
    readonly KorisniciService korisniciService = new KorisniciService();

    protected void Page_Load(object sender, EventArgs e)
    {
        var prijavljen = User.Identity.IsAuthenticated;

        var isAdmin = korisniciService.DaliJeKorisnikAdmin(User.Identity.Name);

        if(!prijavljen && !isAdmin)
            Response.Redirect("../Error/Default.aspx");

        var korisnik = korisniciService.UzmiKorisnikaPoKorisnickomImenu(User.Identity.Name);
        lblDobrodosli.Text = "Dobrodosli " + korisnik.Ime + " " + korisnik.Prezime;

        if (!IsPostBack)
        {
            var korisnici = korisniciService.UzmiSveKorisnike();

            gvKorisnici.DataSource = korisnici;
            gvKorisnici.DataBind();
        }
    }

    protected void GridViewKorisnici_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int id = Convert.ToInt32(gvKorisnici.DataKeys[e.RowIndex].Values[0].ToString());
            korisniciService.ObrisiKorisnika(id);
        }
        catch(Exception ex)
        {
            
        }

        gvKorisnici.DataSource = korisniciService.UzmiSveKorisnike();
        gvKorisnici.DataBind();
        
    }

    protected void GridViewKorisnici_OnRowEditing(object sender, GridViewEditEventArgs e)
    {
        gvKorisnici.EditIndex = e.NewEditIndex;
        gvKorisnici.DataSource = korisniciService.UzmiSveKorisnike();
        gvKorisnici.DataBind();
    }

    protected void GridViewKorisnici_OnRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvKorisnici.EditIndex = -1;
        gvKorisnici.DataSource = korisniciService.UzmiSveKorisnike();
        gvKorisnici.DataBind();
    }

    protected void GridViewKorisnici_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var userId = (int)gvKorisnici.DataKeys[e.RowIndex].Value;
        var korisnik = korisniciService.UzmiKorisnikaPoId(userId);

        TextBox ime = (TextBox)gvKorisnici.Rows[e.RowIndex].FindControl("tbIme");
        TextBox prezime = (TextBox)gvKorisnici.Rows[e.RowIndex].FindControl("tbPrezime");
        TextBox korisnickoIme = (TextBox)gvKorisnici.Rows[e.RowIndex].FindControl("tbKorisnickoIme");
        TextBox lozinka = (TextBox)gvKorisnici.Rows[e.RowIndex].FindControl("tbLozinka");
        TextBox email = (TextBox)gvKorisnici.Rows[e.RowIndex].FindControl("tbEmail");

        korisnik.Ime = ime.Text;
        korisnik.Prezime = prezime.Text;
        korisnik.KorisnickoIme = korisnickoIme.Text;
        korisnik.Lozinka = lozinka.Text;
        korisnik.Email = email.Text;

        korisniciService.AzurirajKorisnika(korisnik);

        gvKorisnici.EditIndex = -1;
    }

    protected void GridViewKorisnici_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }

    protected void GridViewKorisnici_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Response.Write(e.CommandArgument.ToString());
        
        if(e.CommandName.Equals("Dodaj"))
        {
            TextBox ime = (TextBox)gvKorisnici.FooterRow.FindControl("tbNovoIme");
            TextBox prezime = (TextBox)gvKorisnici.FooterRow.FindControl("tbNovoPrezime");
            TextBox korisnickoIme = (TextBox)gvKorisnici.FooterRow.FindControl("tbNovoKorisnickoIme");
            TextBox lozinka = (TextBox)gvKorisnici.FooterRow.FindControl("tbNovaLozinka");
            TextBox email = (TextBox)gvKorisnici.FooterRow.FindControl("tbNoviEmail");

            var korisnik = new Korisnici();

            korisnik.Ime = ime.Text;
            korisnik.Prezime = prezime.Text;
            korisnik.KorisnickoIme = korisnickoIme.Text;
            korisnik.Lozinka = lozinka.Text;
            korisnik.Email = email.Text;

            korisniciService.DodajKorisnika(korisnik);

            gvKorisnici.DataSource = korisniciService.UzmiSveKorisnike();
            gvKorisnici.DataBind();
        }
    }
}
